import { loginAPI, getUserAPI, getUserInfoAPI } from '@/api/user'
import { setToken as setTokenTo, getToken, removeToken } from '@/utils/auth'
const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setToken(state, newToken) {
    state.token = newToken
    setTokenTo(newToken)
  },
  setUserInfo(state, newUserInfo) {
    state.userInfo = newUserInfo
  },
  LayOut(state) {
    state.token = ''
    removeToken()
    state.userInfo = {}
  }
}
const actions = {
  // login(store, data) {
  //   return new Promise((resolve, reject) => {
  //     loginAPI(data).then((res) => {
  //       console.log(res)
  //       store.commit('setToken', res.data)
  //       resolve(res)
  //     }).catch((err) => {
  //       console.log(err.message)
  //       reject(err)
  //     })
  //   })
  // },
  async login(store, data) {
    const res = await loginAPI(data)
    store.commit('setToken', res.data)
    return res
  },
  async getUserInfo(store) {
    const { data: res } = await getUserAPI()
    const { data: res1 } = await getUserInfoAPI(res.userId)
    console.log(res)
    console.log(res1)
    const baseResult = {
      ...res,
      ...res1
    }
    store.commit('setUserInfo', baseResult)
    return baseResult
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

// export default {
//   namespaced: true,
//   state: {},
//   mutations: {},
//   actions: {},
//   getters: {}
// }
